package com.zte.cl.handler;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.zte.cl.model.News;
import com.zte.cl.utils.StringUtil;

/**
 * 新闻过滤处理类--相似度过滤
 * @author lh
 *
 */
public class NewsSimilarDegreeHandler extends NewsHandler {
	
	private static final Log log = LogFactory.getLog(NewsSimilarDegreeHandler.class);

	// 相似度
	private double degree;
	
	public NewsSimilarDegreeHandler(double degree) {
		this.degree = degree;
	}
	
	@Override
	public List<News> filterNews(List<News> list) {
		List<News> filteredNewsList = new ArrayList<News>();
		
		for(int i = 0; i < list.size(); i++) {
			News news = list.get(i);
			boolean similar = false;
			if(filteredNewsList.size() > 0) {
				String temp = "";
				double similarDegree = 0.0;
				for(int j = 0; j < filteredNewsList.size(); j++) {
					News filteredNews = filteredNewsList.get(j);
					similarDegree = StringUtil.SimilarDegree(news.getTitle(), filteredNews.getTitle());
					if(similarDegree > degree) {
						similar = true;
						temp = filteredNews.getTitle();
						break;
					}
				}
				
				if(similar) {
					log.info("相似新闻, 相似度:" + similarDegree + "!title1:" + news.getTitle() + ", title2:" + temp);
					continue;
				} else {
					filteredNewsList.add(news);
				}
			} else {
				filteredNewsList.add(news);
			}
		}
		
		if(getNextHandler() != null) {
			return getNextHandler().filterNews(filteredNewsList);
		} else {
			return filteredNewsList;
		}
	}

}
